<template>
    <orh-table reload  @on-reload="reload" add="add" @on-add="moralAdd" del="del" @on-del="delConfirm" pages :pageDate="pageDate" @on-page-change="reload">
        <RadioGroup v-model="dataState" @on-change="stateChange" style="margin-left: 10px;" slot="returntosuperior">
            <Radio :label="1">全部</Radio>
            <Radio :label="2">我的添加的值班</Radio>
        </RadioGroup>

        <Button slot="returntosuperior" @click="BatchPrinting" style="margin-left: 10px;" size="small" type="info">批量查看</Button>

        <Table border stripe :columns="columns" @on-selection-change="selectOne" :data="data" :loading="tableload" slot="table"></Table>

        <orh-sidebar v-model="show" :title="titles" :exports="status" @on-exports="exportsClick" :printer="status || content === 'BatchPrinting'" @on-printer="printingClick"  scale slot="others">
            <DatePicker @on-change="dateChange" type="daterange" slot="sidebars" size="small" show-week-numbers placeholder="请选择查询时间段" style="width: 200px;margin-left: 10px;"></DatePicker>
            <section ref="dutymoduleadmindata">
                <component :is="content" :condition="condition" @requery="requery" ref="adminDutyData"></component>
            </section>
        </orh-sidebar>
        
    </orh-table>
</template>
                
<script>
export default {
    name: 'dutymodule-admindata',
    components: {
        CheckData: resolve => {require(['./CheckData.vue'], resolve);},
        Operation: resolve => {require(['./Operation.vue'], resolve);},
        BatchPrinting: resolve => {require(['./BatchPrinting.vue'], resolve);}
    },
    data () {
        return {
            status: false,
            show: false,
            content: null,
            titles: null,
            tableload:false,
            dataState: 1,
            pageDate: {
                total: 0,
                current_page: 1,
                per_page: 0
            },
            data:[],
            columns:[
                {
                    type: 'selection',
                    width: 60,
                    fixed: 'left',
                    align: 'center'
                },
                {
                    title: '值班领导',
                    minWidth: 150,
                    fixed:'left',
                    align: 'center',
                    render:(h,params)=>{
                        let name = params.row.leader_name.zh;                       
                        return h('div',name)
                    },
                },
                {
                    title: '值班环节干部',
                    minWidth: 150,
                    align: 'center',
                    render:(h,params)=>{
                        let name = params.row.cadredata;
                        if(name.length > 0){
                            let cadre_name = ''
                            name.map(item=>{
                                if(item.cadredata !=null){
                                    cadre_name += item.cadredata.teacher_name.zh + '  '
                                }
                                
                            })
                            return h('div',cadre_name)
                        }                        
                    },
                },
                {
                    title: '值班时间',
                    minWidth: 150,
                    align: 'center',
                    key: 'created_at',
                },
                {
                    title: '星期',
                    minWidth: 150,
                    align: 'center',
                    key: 'week',
                },
                {
                    title: '操作',
                    key: 'action',
                    width: 180,
                    fixed: 'right',
                    align: 'center',
                    render: (h, params) => {
                        let state = params.row.usermtype
                        return h('div', [
                            h('Button', {
                                props: {
                                    type: 'success',
                                    size: 'small',
                                },
                                style: {
                                    marginRight: '5px',
                                },
                                on: {
                                    click: () => {
                                        this.status = true
                                        this.CheckData(params.row.data_log_id)
                                    },
                                },
                            }, '查看'),
                            h('Button', {
                                directives: [
                                    {
                                        name: 'button',
                                        value: 'edit',
                                    }
                                ],
                                props: {
                                    type: 'primary',
                                    size: 'small',
                                    disabled: state === 1 ? false : true
                                },
                                style: {
                                    marginRight: '5px',
                                },
                                on: {
                                    click: () => {
                                        this.status = false
                                        this.updata(params.row.data_log_id)
                                    },
                                },
                            }, '修改'),
                            h('Poptip', {
                                props: {
                                    confirm: true,
                                    transfer: true,
                                    title: '您确定要删除这条数据吗?',
                                    type: 'error',
                                    size: 'small',
                                    placement:'left-end'
                                },
                                style: {
                                    textAlign: 'left',
                                },
                                on: {
                                    'on-ok': () => {
                                        this.remove(params.row.data_log_id)
                                    }
                                }
                            }, [
                                h('Button', {
                                    directives: [
                                        {
                                            name: 'button',
                                            value: 'del',
                                        }
                                    ],
                                    props: {
                                        type: 'error',
                                        size: 'small',
                                        disabled: state === 1 ? false : true
                                    }
                                }, '删除')
                            ]),
                        ]);
                    }
                },
            ],
            url: 'api/watchlist/v1/admininspecttermdata/list',
            delUrl: 'api/watchlist/v1/admininspecttermdata/del/',
            del: [],
            condition: null,

            meurl: 'api/watchlist/v1/admininspecttermdata/addverify'
        }
    },

    methods: {
        stateChange () {
            this.reload (1)
        },
        dateChange (date) {
            let arr = date
            if (date[0] === '') arr = false
            this.$refs.adminDutyData.reloadData (arr)
        },
        BatchPrinting () {
            this.titles = '查看数据'
            this.show = true
            this.content = 'BatchPrinting'
        },
        
        /************************************************************
         * 
         * 打印数据
         * 
         ************************************************************/
        exportsClick () {
            this.$refs.adminDutyData.printingData ()
        },
        
        /************************************************************
         * 
         * 打印数据
         * 
         ************************************************************/
        printingClick () {
            // this.$print(this.$refs.dutymoduleadmindata)
            this.$print(this.$refs.adminDutyData)
        },
        
        /************************************************************
         * 
         * 删除确认
         * 
         ************************************************************/
        delConfirm () {
            if (this.del.length) {
                this.$Modal.confirm({
                    title: '删除确认',
                    closable: true,
                    loading: true,
                    content: '您确定要删除这些数据吗?',
                    onOk: () => {
                        this.delSubmit ()
                    }
                })
            }
        },
        
        /************************************************************
         * 
         * 查看详情
         * 
         ************************************************************/
        CheckData (index) {
            this.condition = index
            this.titles = '查看数据'
            this.show = true
            this.content = 'CheckData'
        },
        
        /************************************************************
         * 
         * 添加数据
         * 
         ************************************************************/
        moralAdd () {
            this.addStatus ()
        },
        addStatus () {
            this.$http.get(this.meurl).then(val=>{
                if (!val.data.length) {
                    this.status = false
                    this.condition = null
                    this.titles = '添加数据'
                    this.show = true
                    this.content = 'Operation'
                } else {
                    this.pageDate.per_page = 1
                    this.pageDate.total = 1
                    this.pageDate.current_page = 1
                    this.data = val.data 
                    this.tableload = false
                    this.$Message.warning({
                        content: '您今天已检查，请修改',
                        duration: 3
                    })
                }
            }).catch(err => {
                this.tableload = false
            })
        },
        
        /************************************************************
         * 
         * 单条数据删除
         * @param   {index}            当前数据id
         * 
         ************************************************************/
        remove (index) {
            this.del = []
            this.del.push(index)
            this.delSubmit()
        },
        
        /************************************************************
         * 
         * 修改数据
         * @param   {index}            当前数据id
         * 
         ************************************************************/
        updata (index) {
            this.condition = index
            this.titles = '修改数据'
            this.show = true
            this.content = 'Operation'
        },
        
        /************************************************************
         * 
         * 批量删除数据
         * @param   {index}            所选数据id[array]
         * 
         ************************************************************/
        selectOne (index) {
            this.del = []
            index.forEach(item => {
                this.del.push(item.data_log_id)
            });
        },
        
        /************************************************************
         * 
         * 删除操作
         * 
         ************************************************************/
        delSubmit () {
            if(this.del.length == 0){
                this.$Message.info('请选择删除项')
                this.prompt = false
                return
            }
            this.del = JSON.stringify(this.del)
            this.$http.delete(this.delUrl + this.del).then(val=>{
                this.$Modal.remove ()
                this.reload (this.pageDate.current_page)
            }).catch(err => {
                this.$Modal.remove ()
            })
        },
        
        /************************************************************
         * 
         * 添加，修改数据成功后关闭组件并刷新数据
         * @param   {state}            状态
         * 
         ************************************************************/
        requery (state) {
            if(state){
                this.show = false
                this.reload (this.pageDate.current_page)
            }
        },
        
        /************************************************************
         * 
         * 刷新数据
         * @param   {index}            分页
         * 
         ************************************************************/
        reload (index) {
            this.data = []
            this.tableload = true
            this.queryData(index?index:1)
        },
        
        /************************************************************
         * 
         * 列表数据
         * @param   {index}             分页
         * 
         ************************************************************/
        queryData (index) {
            this.tableload = true;
            let params = {
                user: this.dataState,
                page: index
            }
            this.$http.get(this.url, { params }).then(val=>{
                this.pageDate.per_page = val.pages.per_page
                this.pageDate.total = val.pages.total
                this.pageDate.current_page = val.pages.current_page
                this.data = val.data 
                this.tableload = false
            }).catch(err => {
                this.tableload = false
            })
        },
    },
    created () {
        this.reload (1)
    },
}
</script>